// package-product/pages/earning-record-detail/index.js
const app = getApp();
import { ManageTargetInfoUpdateMixins } from '../../../behaviors/page';
import { ACCOUNT_LOG_CATEGORY, ACCOUNT_LOG_CATEGORY_TEXT } from '../../../constants/common';
import { PACKAGE_ORDER_PAGE } from '../../../constants/route';
import { navTo } from '../../../common/env';

Page({
    mixins: [ManageTargetInfoUpdateMixins],
    data: {
        ACCOUNT_LOG_CATEGORY,
        categoryText: ACCOUNT_LOG_CATEGORY_TEXT,
        recordInfo: {},
        extInfo: {},
        isRefund: false,
        isWithdraw: false,
        isRollBack: false,
        isBonus: false,
        orderNumber: '',
        priceValue: '',
    },
    onLoad(options) {
        app.event.addListener('sendOwnerEarningDetail', this._handleDetail.bind(this));
    },
    onUnload() {
        app.event.removeListener('sendOwnerEarningDetail');
    },
    _handleDetail(recordInfo) {
        const { extInfo } = recordInfo;
        const isRefund = recordInfo.category == ACCOUNT_LOG_CATEGORY.refund;
        const isWithdraw = recordInfo.category == ACCOUNT_LOG_CATEGORY.withdraw;
        const isRollBack = recordInfo.category == ACCOUNT_LOG_CATEGORY.bonusRollback;
        const isBonus = recordInfo.category == ACCOUNT_LOG_CATEGORY.bonus;
        this.setData({
            recordInfo,
            isRefund,
            isWithdraw,
            isRollBack,
            isBonus,
            extInfo,
            orderNumber: isRefund ? extInfo.afterSaleSerialNumber : extInfo.orderSerialNumber,
            priceValue: `${recordInfo.amount > 0 ? '+' : '-' }￥${Math.abs(recordInfo.amount).toFixed(2)}`,
        });
    },
    gotoOrderDetail() {
        const { isRefund, orderNumber, extInfo } = this.data;
        navTo(isRefund ? PACKAGE_ORDER_PAGE.expandAfterSaleDetail : PACKAGE_ORDER_PAGE.expandOrderDetail, {
            orderNumber,
            spnId: extInfo.spnId,
        });
    },
})
